package com.ikkong.api.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.kit.HttpKit;

/**
 * 短信相关的工具类*
 * 
 * @author malongbo
 */
public class SMSUtils {

	/**
	 * 检测手机号有效性*
	 * 
	 * @param mobile
	 *            手机号码
	 * @return 是否有效
	 */
	public static final boolean isMobileNo(String mobile) {
		Pattern p = Pattern.compile("^1[3|4|5|7|8][0-9]\\d{4,8}$");
		Matcher m = p.matcher(mobile);
		return m.matches();
	}

	/**
	 * 生成短信验证码*
	 * 
	 * @param length
	 *            长度
	 * @return 指定长度的随机短信验证码
	 */
	public static final String randomSMSCode(int length) {
		boolean numberFlag = true;
		String retStr = "";
		String strTable = numberFlag ? "1234567890"
				: "1234567890abcdefghijkmnpqrstuvwxyz";
		int len = strTable.length();
		boolean bDone = true;
		do {
			retStr = "";
			int count = 0;
			for (int i = 0; i < length; i++) {
				double dblR = Math.random() * len;
				int intR = (int) Math.floor(dblR);
				char c = strTable.charAt(intR);
				if (('0' <= c) && (c <= '9')) {
					count++;
				}
				retStr += strTable.charAt(intR);
			}
			if (count >= 2) {
				bDone = false;
			}
		} while (bDone);
		return retStr;
	}

	/**
	 * 发送短信验证码*
	 * 
	 * @param mobile
	 *            手机号码
	 * @param code
	 *            验证码
	 * @return 是否发送成功
	 * @throws UnsupportedEncodingException
	 */
	public static final boolean sendSMS(String mobile, String content) {
		// DefaultApi apiInstance = new DefaultApi();
		content = "【智能识别系统】" + content; // String | 短信内容，需要UTF-8编码
		// 格式化时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(new Date());
		String PostData;

		try {
			PostData = "action=send&userid=5576&account=windlee09&password=123123&mobile="
					+ mobile
					+ "&sendtime="
					+ time
					+ "&content="
					+ java.net.URLEncoder.encode(content, "utf-8");
			String ret = HttpKit.post("http://sms.kingtto.com:9999/sms.aspx",
					PostData);

			/*
			 * PostData =
			 * "userid=1314&account=bdgxqc&password=bd2w6TY78a&mobile=" + mobile
			 * + "&sendTime=" + time + "&content="+
			 * java.net.URLEncoder.encode(content, "utf-8"); String ret =
			 * HttpKit.post( "http://web.28inter.com:8888/sms.aspx?action=send",
			 * PostData);
			 */
			System.out.println(ret);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return true;

	}

	/**
	 * @param urlAll
	 *            :请求接口
	 * @param httpArg
	 *            :参数
	 * @return 返回结果
	 */
	public static String request(String httpUrl, String httpArg) {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();
		httpUrl = httpUrl + "?" + httpArg;

		try {
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("apikey",
					"a84cab502bc270daa1e178507243e30f");
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String strRead = null;
			while ((strRead = reader.readLine()) != null) {
				sbf.append(strRead);
				sbf.append("\r\n");
			}
			reader.close();
			result = sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
